package com.tr.rentcar.controller.goods;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.bean.UserRecord;
import com.tr.rentcar.model.dao.OrderDao;
import com.tr.rentcar.model.dao.OrderNoDao;
import com.tr.rentcar.model.dao.OrderRecordDao;
import com.tr.rentcar.model.dao.UserDao;
import com.tr.rentcar.model.dao.UserDiscountDao;
import com.tr.rentcar.model.dao.UserJfDao;
import com.tr.rentcar.model.dao.UserRecordDao;

@WebServlet("/orderok")
public class OrderSuccessController extends HttpServlet{

	/**
	 * 下订单成功的servlet
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date=new Date();
			User user=(User) req.getSession().getAttribute("userinfo");
			int uid=user.getUid();
			String omoney=req.getParameter("omoney");
			String ocontent=req.getParameter("ocontent");
			String cid=req.getParameter("cid");
			String uaName=req.getParameter("uaName");
			String uaIdentity=req.getParameter("uaIdentity");
			String uaTel=req.getParameter("uaTel");
			Date takeTime=sdf.parse(req.getParameter("takeTime"));
			Date returnTime=sdf.parse(req.getParameter("returnTime"));
			String takePlace=req.getParameter("takePlace");
			String returnPlace=req.getParameter("returnPlace");
			
			String jf=req.getParameter("jf");
			String did=req.getParameter("did");
			String type=req.getParameter("type");
			if("1".equals(type)){
				//获取订单编号
				OrderNoDao ond=new OrderNoDao();
				int no;
				if(ond.getNo()==null){
					no=0;
				}else{
					no=ond.getNo()+1;
				}
				ond.insertNo(no);
				String ono="ZC"+new SimpleDateFormat("yyyyMMddHHmmss").format(date)+String.format("%03d",no);
				//修改用户积分数目并插入积分消费记录
				if(Integer.parseInt(jf)!=0){
					new UserDao().updateJF(Integer.parseInt(jf), uid);
					new UserJfDao().insertRecord(date, Integer.parseInt("-"+jf), "消费", uid);
				}
				//删除使用过的优惠券
				if(did!=null){
					new UserDiscountDao().deleteDiscount(uid, Integer.parseInt(did));
				}
				//插入一条订单记录
				boolean status3=new OrderDao().insertOrder(ono, omoney, ocontent, cid, uaName, uaIdentity,uaTel, uid, takeTime, returnTime, takePlace, returnPlace);
				int oid=new OrderDao().findByOno(ono).getoId();
				//根据oid插入订单操作记录
				new OrderRecordDao().insertRecord("用户下单",user.getUname(), oid);
				resp.getWriter().println(ono);
			}else{
				req.setAttribute("user", new UserDao().findByid(uid));
				
				req.getRequestDispatcher("WEB-INF/jsp/orderOK.jsp").forward(req, resp);
			}
			
		} catch (Exception e) {
			req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
			e.printStackTrace();
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			User user=(User) req.getSession().getAttribute("userinfo");
			int uid=user.getUid();
			String omoney=req.getParameter("omoney");
			String ono=req.getParameter("ono");
			//修改余额
			boolean status1=new UserDao().updateUmoney(Double.parseDouble(omoney), uid);
			//修改订单状态
			OrderDao od=new OrderDao();
			Integer oid=od.findByOno(ono).getoId();//获取当前订单id
			boolean status2=od.updatestatus("未处理", oid);
			//根据oid插入订单操作记录
			new OrderRecordDao().insertRecord("用户支付全款",user.getUname(), oid);
			//产生一条消费记录
			UserRecord ur=new UserRecord();
			ur.setuId(uid);
			ur.setUrWay("扣费");
			ur.setUrMoney(Double.parseDouble(omoney));
			ur.setUrdoWay("用户");
			ur.setUrName(new UserDao().findByid(uid).getUname());
			boolean status3=new UserRecordDao().save(ur);
			//传回去
			PrintWriter out=resp.getWriter();
			out.println(status1&&status2 &&status3);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
